package com.tianruan.dc.modules.sys.user.api;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianruan.dc.common.utils.StringUtils;
import com.tianruan.dc.modules.sys.user.entity.dto.UserDTO;
import com.tianruan.dc.modules.sys.user.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.tianruan.dc.modules.sys.user.entity.db.MajorDO;
import com.tianruan.dc.modules.sys.user.service.IMajorService;
import com.tianruan.dc.common.utils.response.VResponse;



/**
 *
 * @PS 专业表
 *
 */
@RestController
@RequestMapping("/api/sys/user/major")
public class MajorController {

    @Autowired
    private IMajorService majorService;

    @PreAuthorize("hasAnyAuthority('admin', 'officeAdmin')")
    @GetMapping
    public VResponse<List<MajorDO>> list(@RequestParam(value = "officeId", required = false) String officeId) {
        if (UserUtils.hasAuthority("officeAdmin")) {
            officeId = UserUtils.getUser().getOfficeId();
        }
        QueryWrapper<MajorDO> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(officeId)) {
            queryWrapper.eq("office_id", officeId);
        }
        return VResponse.success(majorService.list(queryWrapper));
    }

    /**
     * 获取学院专业
      * @return
     */
    @PreAuthorize("hasAnyAuthority('officeAdmin')")
    @GetMapping("/office")
    public VResponse<List<MajorDO>> officeList() {
        UserDTO user = UserUtils.getUser();
        QueryWrapper<MajorDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("office_id", user.getOfficeId());
        return VResponse.success(majorService.list(queryWrapper));
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public VResponse info(@PathVariable("id") String id){
		MajorDO major = majorService.getById(id);

        return VResponse.success("major", major);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public VResponse save(@RequestBody MajorDO major){
		majorService.save(major);

        return VResponse.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public VResponse update(@RequestBody MajorDO major){
		majorService.updateById(major);

        return VResponse.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public VResponse delete(@RequestBody String[] ids){
		majorService.removeByIds(Arrays.asList(ids));

        return VResponse.success();
    }

}
